import type { FC, PropsWithChildren } from 'react'
import useAppStore, { selectToken } from '@/store/app-store.ts'
import { Navigate, useLocation } from 'react-router-dom'

const AuthRoot: FC<PropsWithChildren> = ({ children }) => {
  const token = useAppStore(selectToken)
  const location = useLocation()

  if (token) {
    return <>{children}</>
  } else {
    const nextURL = location.pathname + location.search
    return <Navigate to={'/login?from=' + nextURL} />
  }
}

export default AuthRoot
